<?php

namespace Cktadmin\Controller;

use Think\Controller;

class LoginController extends Controller {

    private $table = 'admin_user';

    public function index() {
        if (session('?user_info') == true) {
            $this->redirect('/' . MODULE_NAME);
        }
        if (IS_POST) {
            $username = I('post.username', '', 'trim');
            $password = I('post.password', '', 'trim');
            $captcha = I('post.captcha', '', 'trim');
            strlen($username) < 1 && $this->error('请输入用户名!');
            strlen($password) < 1 && $this->error('请输入密码!');
//            !$captcha && $this->error('请输入验证码');
//            !check_verify($captcha) && $this->error('验证码错误！', $url);
            $user_info = M($this->table)->where(array('username' => $username, 'is_deleted' => 1))->find();
            empty($user_info) && $this->error('登录账号不存在，请重新输入!');
            $user_info['password'] !== encryption($password . $username) && $this->error('登录密码与账号不匹配，请重新输入!');
            empty($user_info['status']) && $this->error('账号已经被禁用，请联系管理!');
//            unset($user_info['password']);
//            cookie('username', $user_info['username'], 86400);
            session('user_info', $user_info);
//            D('User')->updateLogin($user_info['id']);
            $this->success('登录成功，正在进入系统...', U('/' . MODULE_NAME));
        }
        $this->display();
    }

    /**
     * 验证码
     */
    public function verify() {
        $Verify = new \Think\Verify();
        $Verify->codeSet = '2345678ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnprstuvwxyz'; // 设置验证码字符为纯数字   
        $Verify->length = 4;
        $Verify->useNoise = true;
        $Verify->useCurve = true;
        $Verify->fontttf = "4.ttf";
        //$Verify->bg = array(196, 223, 246);
        $Verify->entry();
    }

    public function logout() {
        session('user_info', null);
        session('?user_info') == false && $this->success('退出登录成功！', U('/' . MODULE_NAME));
    }

}
